package com.iwith.dongmoon.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.iwith.dongmoon.gsonlist.BoardList;
import com.iwith.dongmoon.gsonlist.BoardListItem;
import com.iwith.dongmoon.listadpater.FourTextListAdapter;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.SessionManager;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class GroupListViewActivity extends Activity implements OnClickListener{

	private Button writeButton;
	private ListView groupListView;
	private FourTextListAdapter groupListAdapter;
	private Button prevBt;
	
	private String ca_name;
	private String userid;
	
	private ImageView title;
	
	private SessionManager mSessionManager=null;
	
	private Context mContext;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    // TODO Auto-generated method stub
	    setContentView(R.layout.grouplist);
	    mSessionManager=new SessionManager(this);
	}
	
	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}
	
	private void Initialize(){
		mContext = this;
		
		title=(ImageView)findViewById(R.id.title);
		writeButton=(Button)findViewById(R.id.grouplist_writeBt);
		groupListView=(ListView)findViewById(R.id.grouplist_listview);
		prevBt=(Button)findViewById(R.id.backbtn);
		
		ca_name = getIntent().getExtras().getString("ca_name");
		userid=mSessionManager.getUserid();
		
		setData();
		
		writeButton.setOnClickListener(this);
		prevBt.setOnClickListener(this);
		title.setOnClickListener(this);
	}
	private void setData(){
		ThreadInterface threadInterface=new ThreadInterface(){
			BoardList boardList;
			RequestGson requestGson=new RequestGson();
			public void onThread() {
				// TODO Auto-generated method stub
				String api=ApiCode.API_GROUP;
				boardList=requestGson.getBoardGsonBySendingAPI(mContext, api, "GET", "/"+ca_name);
			}

			public void onThreadEnd() {
				// TODO Auto-generated method stub
				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					groupListAdapter=new FourTextListAdapter(mContext, R.layout.listitem_fourtext, (ArrayList<BoardListItem>)boardList.getList());
					groupListView.setAdapter(groupListAdapter);
				} else {
					requestGson.alertToast(mContext, "데이터 로딩 실패");
				}
			}
		};
		
		ThreadTask task = new ThreadTask((Activity)mContext, threadInterface);
		task.execute();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==writeButton.getId()){
			Intent intent=new Intent(this, UpdateNewsActivity.class);
			intent.putExtra("userid", userid);
			intent.putExtra("boardname", ca_name);
			intent.putExtra("code", ApiCode.GROUP);
			startActivity(intent);
		} else if(v.getId()==title.getId()){
			Intent intent=new Intent(this, MainIconView.class);
			intent.putExtra("flag", true);
			startActivity(intent);
		} else if(v.getId()==prevBt.getId()){
			finish();
		}
	}
}
